Skip to content

Make the ffi crate no_std#6022

Merged
davidhewitt merged 8 commits into
PyO3:mainfrom
Person-93:no_std_ffi
May 12, 2026
Merged

Make the ffi crate no_std#6022
davidhewitt merged 8 commits into
PyO3:mainfrom
Person-93:no_std_ffi

Conversation

@Person-93

@Person-93 Person-93 commented May 8, 2026

Copy link
Copy Markdown
Contributor

I've added no_std to the python-ffi crate.

Most of the changes in this PR are a search-and-replace from std:: to core::. The only other change I've made is in the datetime file. It used the Once type from std. I've updated it to use AtomicBool instead.

This is the first PR from this converstaion: #5856 (comment)

@codspeed-hq

codspeed-hq Bot commented May 8, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 125 untouched benchmarks


Comparing Person-93:no_std_ffi (ee47c67) with main (e742ed2)

Open in CodSpeed

@Person-93

Copy link
Copy Markdown
Contributor Author

The ffi crate currently uses std::thread::park to work around the interpreter calling pthread_exit for python versions older than 3.14.

#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
struct HangThread;
#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
impl Drop for HangThread {
fn drop(&mut self) {
loop {
std::thread::park(); // Block forever.
}
}
}

I can think of two ways to handle this.

  • move the usage into the main crate
  • use a feature-flag to conditionally use a separate crate that can sleep in no_std

The first way is technically a breaking change if anyone is using the PyGILState_Ensure method from the ffi crate as they would have to deal with pinned stack objects some other way, but I think it's simpler, so I will go with that one unless you object.

@Person-93 Person-93 force-pushed the no_std_ffi branch 2 times, most recently from 3f9162a to 48b06f9 Compare May 9, 2026 00:27

@davidhewitt davidhewitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, overall this looks good. Couple of suggestions / thoughts / questions about the nontrivial bits.

Comment thread pyo3-ffi/src/pystate.rs
Comment thread pyo3-ffi/src/lib.rs
Comment thread pyo3-ffi/src/datetime.rs
@ngoldbaum

Copy link
Copy Markdown
Contributor

Neat, I'm glad this is mostly mechanical. I'll try to give this a once-over.

@ngoldbaum ngoldbaum left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with all of David's comments. I also think we need a CI job that compiles the crate with no_std. Might be worth adding that to the nox configuration so all the CI jobs do it if it's a pretty quick build.

@Person-93

Person-93 commented May 10, 2026

Copy link
Copy Markdown
Contributor Author

I agree with all of David's comments. I also think we need a CI job that compiles the crate with no_std. Might be worth adding that to the nox configuration so all the CI jobs do it if it's a pretty quick build.

The ffi crate will always compile with no_std if python >= 3.14 or wasm build. It's not feature-gated.

@davidhewitt davidhewitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, couple of tiny questions

Comment thread newsfragments/6022.added.md Outdated
Comment thread pyo3-ffi/src/lib.rs

@davidhewitt davidhewitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I fixed some merge conflicts and one tiny case I spotted on debug builds, let's send this to merge and see if it lands!

@davidhewitt davidhewitt enabled auto-merge May 12, 2026 06:14
@davidhewitt davidhewitt added this pull request to the merge queue May 12, 2026
Merged via the queue into PyO3:main with commit c184871 May 12, 2026
87 of 88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants